# -*- coding: utf-8 -*-
import ast, json
import uuid

import ast

from django.views import View
# 导入返回响应
from SaaSDevelTool.data_return_response import create_response
# 导入错误信息装饰器
from SaaSDevelTool.data_return_response import error_message_detection
# 获取数据库连接
from SaaSDevelTool.data_mysql_data import db

"""
页面权限添加
"""


class SaaSDevelResourceRightCreate(View):
    # @error_message_detection
    def post(self, request):
        # 获取权限类型
        right_type = request.POST.get("right_type")
        # 获取权限名称
        right_name = request.POST.get("right_name")
        # 获取权限名
        name = request.POST.get("name")
        # 获取权限等级
        right_level = request.POST.get("right_level")
        # 获取资源编号
        resource_id = request.POST.get("resource_id")
        # 判断参数是否完整
        if not all([right_type, right_name, name, right_level, resource_id]):
            return create_response(code=400, msg="未传递必传参数")

        # 判断 right_level是否是整数
        try:
            int(right_level)
        except Exception as err:
            return create_response(code=200, msg="权限等级必须是整数。")

        # 查询里面是否有存在的数据
        query_base_info_resource_right_sql = """
            select
                right_id,
                right_name,
                right_code,
                right_level,
                resource_id,
                right_type
            from
                db_saas_center.sum_page_resource_right 
            where 
                right_type = {}
            and 
                resource_id = {} 
            and
                right_name = '{}'
                """.format(right_type, resource_id, right_name)
        # 获取查询出来的数据
        query_base_info_resource_right_data = db.findOne(query_base_info_resource_right_sql)
        # 判断当前数据如果存在
        if query_base_info_resource_right_data:
            # 执行修改操作
            query_right_level = ast.literal_eval(query_base_info_resource_right_data['right_level'])
            # 判断当前名称是否存在对应的json字段中
            name_exists = any(item['name'] == name for item in query_right_level)
            if name_exists:
                # 如果重复，进行的操作
                return create_response(code=400, msg="当前权限名已存在对应的权限名称中，不可重复添加")

            # 如果不存在，新增
            else:
                # 新增对应的接口
                query_right_level.append({"name": name, "grade": right_level, "uuid": str(uuid.uuid4())})
                # 将值进行转码
                query_right_level = json.dumps(query_right_level, ensure_ascii=False).replace("'", '"')
                # 拼写sql语句
                create_base_info_resource_right_sql = """
                    UPDATE 
                        db_saas_center.sum_page_resource_right
                    SET 
                        right_level = '{}' 
                    WHERE 
                        right_id = {} 
                    ;""".format(query_right_level, query_base_info_resource_right_data['right_id'])
                db.update(create_base_info_resource_right_sql)
                return create_response(code=200, msg="权限添加成功")
        # 如果不存在
        else:
            # 生成值进行插入
            right_level = [{"name": name, "grade": right_level, "uuid": str(uuid.uuid4())}]
            print("right_level\n",right_level)
            # 转码操作
            query_right_level = json.dumps(right_level, ensure_ascii=False).replace("'", '"')
            # 拼接sql语句
            create_base_info_resource_right_sql = """
                INSERT INTO 
                    db_saas_center.sum_page_resource_right
                    (right_name, right_code, right_level, resource_id, right_type)
                VALUES
                    ('{}', '{}', '{}', {}, {});
            """.format(right_name, str(uuid.uuid4()), query_right_level, resource_id, right_type)
            db.update(create_base_info_resource_right_sql)
            # 返回对应的值
            return create_response(code=200, msg="当前页面权限添加成功。")
